package org.scboy.dbms.service.imp;

import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.DbColumnDao;
import org.scboy.dbms.dao.DbIndexDao;
import org.scboy.dbms.dao.DbTableDao;
import org.scboy.dbms.service.DbTableService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.scboy.utils.enums.coobi.OrderType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.lang.UUID;

@Service
public class DbTableServiceImpl implements DbTableService {

	@Autowired
	EntityFactory factory;
	
	@Override
	public Page page(Model params) {
		DbTableDao dao=factory.getDao(DbTableDao.class);
		params.orderBy("name");
		return dao.page(params);
	}

	@Override
	@Transactional
	public boolean add(Model params) {
		DbTableDao dao=factory.getDao(DbTableDao.class);
		DbColumnDao columnDao=factory.getDao(DbColumnDao.class);
		DbIndexDao indexDao=factory.getDao(DbIndexDao.class);
		params.add("id", UUID.randomUUID().toString());
		
		List<Model> columns=params.getList("columns");
		if(columns.size()==0) {
			return false;
		}
		boolean checkPrimary=false;
		for (Model column : columns) {
			if(column.getBoolean("colPrimary")) {
				checkPrimary=true;
				break;
			}
		}
		
		/**
		 * 没有主键 不让创建表
		 */
		if(!checkPrimary) {
			return false;
		}
		
		for (Model column : columns) {
			column.add("id", UUID.randomUUID().toString());
			column.add("tableId", params.getString("id"));
			columnDao.insert(column);
		}

		List<Model> indexs=params.getList("indexs");
		for (Model index : indexs) {
			index.add("id", UUID.randomUUID().toString());
			index.add("tableId", params.getString("id"));
			indexDao.insert(index);
		}
		
		int count= dao.insert(params);
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	@Transactional
	public boolean update(Model params) {
		DbTableDao dao=factory.getDao(DbTableDao.class);
		DbColumnDao columnDao=factory.getDao(DbColumnDao.class);
		DbIndexDao indexDao=factory.getDao(DbIndexDao.class);
		params.add("tabVersion", params.getInt("tabVersion")+1);
		List<Model> columns=params.getList("columns");
		if(columns.size()==0) {
			return false;
		}
		boolean checkPrimary=false;
		for (Model column : columns) {
			if(column.getBoolean("colPrimary")) {
				checkPrimary=true;
				break;
			}
		}
		/**
		 * 没有主键 不让创建表
		 */
		if(!checkPrimary) {
			return false;
		}
		//先删除旧的配置
		columnDao.delete(Model.create().add("tableId", params.getString("id")));
		for (Model column : columns) {
			column.add("id", UUID.randomUUID().toString());
			column.add("tableId", params.getString("id"));
			columnDao.insert(column);
		}

		List<Model> indexs=params.getList("indexs");
		
		//先删除旧的配置
		indexDao.delete(Model.create().add("tableId", params.getString("id")));
		for (Model index : indexs) {
			index.add("id", UUID.randomUUID().toString());
			index.add("tableId", params.getString("id"));
			indexDao.insert(index);
		}

		int count= dao.update(params);
		
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public Model get(String id) {
		DbTableDao dao=factory.getDao(DbTableDao.class);
		return dao.get(Model.create().add("id", id));
	}

	@Override
	@Transactional
	public boolean delete(String id) {
		DbTableDao dao=factory.getDao(DbTableDao.class);
		DbColumnDao columnDao=factory.getDao(DbColumnDao.class);
		DbIndexDao indexDao=factory.getDao(DbIndexDao.class);
		//删除列配置
		columnDao.delete(Model.create().add("tableId", id));
		//删除索引
		indexDao.delete(Model.create().add("tableId", id));
		
		int count= dao.delete(Model.create().add("id", id));
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public List<Model> list(String appName) {
		DbTableDao dao=factory.getDao(DbTableDao.class);
		return dao.select(Model.create().add("serverName", appName));
	}

	@Override
	public Model getConfig(String id) {
		DbTableDao dao=factory.getDao(DbTableDao.class);
		DbColumnDao columnDao=factory.getDao(DbColumnDao.class);
		DbIndexDao indexDao=factory.getDao(DbIndexDao.class);
		Model table=dao.get(Model.create().add("id", id));
		//查询列
		table.add("columns", columnDao.select(Model.create().add("tableId", id).orderBy("colPrimary",OrderType.DESC).orderBy("name")));
		//查询索引
		table.add("indexs", indexDao.select(Model.create().add("tableId", id).orderBy("idxName")));
		return table;
	}

}
